﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarStockManager.Model
{
    public class Bar
    {
        public Bar()
        {
            Stock = new Dictionary<Drink, float>();
        }

        public void Add(Drink drink, float volume)
        {
            var key = Find(drink);
            if (key != null)
            {

                Stock[key] += volume;
            }
            else
            {
                Stock.Add(drink, volume);
            }
        }

        private Drink Find(Drink drink)
        {
            return Stock.Keys.FirstOrDefault(d => d.Name == drink.Name);
        }

        public void Sell(Drink drink, float volume)
        {
            Coctail coctail = new Coctail();
            coctail.Ingridients.Add(drink, volume);

            Sell(coctail);
        }

        public void Sell(Coctail coctail, int count = 1)
        {
            Drink currentDrink;
            foreach (var ing in coctail.Ingridients)
            {
                currentDrink = Find(ing.Key);
                if (currentDrink == null)
                {
                    throw new DrinkNotFoundException(String.Format("{0} not found in stock.", ing.Key.Name));
                }
                else
                {
                    float volume = count * ing.Value;
                    if (Stock[currentDrink] < volume)
                    {
                        throw new NotEnoughVolumeException("Not enough volume in stock.");
                    }
                    else
                    {
                        Stock[currentDrink] -= count * ing.Value;
                    }
                }
            }
        }

        private Dictionary<Drink, float> Stock { get; set; }
    }
}
